package client;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class LoginGUI extends JFrame{
	
	private JLabel loginLabel;
	private JTextField loginText;
	
	public LoginGUI() {
		
		super("Mache Login");
		setBounds(400, 200, 150, 150);
		Container cp = this.getContentPane();
		GroupLayout layout = new GroupLayout(getContentPane());
		cp.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		

		
		loginLabel = new JLabel("Username");
		loginLabel.setName("loginLabel");
		
		loginText = new JTextField(8);
		loginText.setName("loginText");
		loginText.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();			
			}
		});
		
		
		
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(loginLabel)
						.addComponent(loginText)));
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(loginLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(loginText)));

		
		setVisible(true);
		pack();
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	
	private void login() {
		String name = loginText.getText();
		loginText.setText("");
		if (name.length() != 0) {
			setVisible(false);
			MacheGUI macheGUI = new MacheGUI(name);
			macheGUI.setVisible(true);
		}
	}
}
